import Cookies from 'js-cookie'

const TokenKey = 'VSE-User-Token';
const UserIsLogin = 'VSE-User-IsLogin';
const UserName = 'VSE-User-Name';
const UserInfo = 'VSE-User-Info';

const user = {
  state: {
    userInfo: Cookies.get(UserInfo) || '',
    status: '',
    isLogin : Cookies.get(UserIsLogin) || 'false',
    token: Cookies.get(TokenKey),
    userName: Cookies.get(UserName) || '',
  },

  mutations: {
    SET_IS_LOGIN:(state, isLogin) =>{
      //设置登录状态
      if(typeof(isLogin) == 'boolean'){
        if(isLogin) state.isLogin='true'
        else state.isLogin='false'
      } else {
        state.isLogin = isLogin;
      }
      Cookies.set(UserIsLogin, isLogin);
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
      Cookies.set(TokenKey, token);
    },
    REMOVE_IS_LOGIN: (state, isLogin) => {
      if(typeof(isLogin) == 'boolean'){
        if(isLogin) state.isLogin='true'
        else state.isLogin='false'
      } else {
        state.isLogin = isLogin;
      }
      Cookies.remove(UserIsLogin);
    },
    SET_USERNAME: (state, username) => {
      state.userName = username;
      Cookies.set(UserName, username);
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
      Cookies.set(UserInfo, userInfo);
    },
  },

  actions: {
    setIsLogin({ commit }, isLogin) {
      commit('SET_IS_LOGIN', isLogin);
    },
    setToken({ commit }, token) {
      commit('SET_TOKEN', token);
    },
    removeIsLogin({ commit }, isLogin) {
      commit('REMOVE_IS_LOGIN', isLogin);
    },
    setUserName({ commit }, username) {
      commit('SET_USERNAME', username);
    },
    setUserInfo({ commit }, userInfo) {
      commit('SET_USERINFO', userInfo);
    },
  }
};

export default user
